MASTER_IP=`hostname -I | awk '{print $2}'`
echo -e "检测到本机IP地址为:${MASTER_IP}\n按回车使用此IP进行安装"
service='centos-release-openstack-queens net-tools vim lrzsz tree screen lsof tcpdump nc mtr nmap python-openstackclient  openstack-selinux  mariadb mariadb-server python2-PyMySQL
'
chek_status(){
    if [[ $? -eq '0' ]];then
        echo -e "$1" "$2" "成功"
    else
        echo -e "$1" "$2" "失败"
    fi
}
systemctl disable NetworkManager  firewalld&&systemctl stop NetworkManager  firewalld
setenforce 0
yum clean all 
yum makecache
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
for i in $service;do
    yum install -y $i
    chek_status "$i" "服务安装"
done
cat <<EOF > /etc/my.cnf.d/openstack.cnf 
[mysqld]
bind-address = ${MASTER_IP} #设置监听的IP地址
default-storage-engine = innodb #设置默认的存储引擎
innodb_file_per_table = on#使用独享表空间
collation-server = utf8_general_ci #服务器的默认校对规则
character-set-server = utf8 #服务器安装时指定的默认字符集设定
max_connections = 4096 #设置MySQL的最大连接数，生产请根据实际情况设置
EOF
systemctl enable mariadb.service
if [ -z `systemctl restart mariadb.service` ];then
	echo -e "数据库服务启动成功"
else
	echo -e "数据库服务启动失败"
	exit 0
fi
echo -e ' --------初始化数据库------------'
expect -c "
spawn /usr/bin/mysql_secure_installation
expect \"Enter current password for root (enter for none):\"
send \"\r\"
expect \"Set root password?\"
send \"y\r\"
expect \"New password:\"
send \"$ALL_PASS\r\"
expect \"Re-enter new password:\"
send \"$ALL_PASS\r\"
expect \"Remove anonymous users?\"
send \"y\r\"
expect \"Disallow root login remotely?\"
send \"n\r\"
expect \"Remove test database and access to it?\"
send \"y\r\"
expect \"Reload privilege tables now?\"
send \"y\r\"
expect eof
"
ALL_PASS='000000'
DB='ksystone glance nova nova_api neutron cinder'
for db in $DB;do
	mysql -uroot -p$ALL_PASS -e "create database IF NOT EXISTS $db;"
	mysql -uroot -p$ALL_PASS -e "GRANT ALL PRIVILEGES ON $db.* TO '$db'@'%' IDENTIFIED BY '$ALL_PASS';"
	mysql -uroot -p$ALL_PASS -e "GRANT ALL PRIVILEGES ON $db.* TO '$db'@'localhost' IDENTIFIED BY '$ALL_PASS';"
done
service='rabbitmq-server openstack-keystone httpd mod_wsgi memcached python-memcached'
for i in $service;do
    yum install -y $i
    chek_status "i" "服务安装"
done 
service='rabbitmq-server.service memcached.service httpd'
for i in $service;do
    systemctl restart $i
    chek_status "$i" "服务启动"
    systemctl enable $i
    chek_status "$i" "服务设置开机启动"
done
rabbitmqctl add_user openstack openstack
rabbitmqctl set_permissions openstack ".*" ".*" ".*"
rabbitmq-plugins enable rabbitmq_management
cat <<EOF > /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS="-l ${MASTER_IP},::1"
EOF
 systemctl start memcached.service
 echo -e "[database]
connection = mysql+pymysql://keystone:keystone@${MASTER_IP}/keystone
[token]
provider = fernet" >  /etc/keystone/keystone.conf
 su -s /bin/sh -c "keystone-manage db_sync" keystone
#mysql -ukeystone -p$ALL_PASS -e " use keystone;show tables;"
keystone-manage fernet_setup --keystone-user keystone --keystone-group keystone
keystone-manage credential_setup --keystone-user keystone --keystone-group keystone
keystone-manage bootstrap --bootstrap-password admin --bootstrap-admin-url http://${MASTER_IP}:35357/v3/ --bootstrap-internal-url http://${MASTER_IP}:35357/v3/ --bootstrap-public-url http://${MASTER_IP}:5000/v3/ --bootstrap-region-id RegionOne
sed -i 's/ServerName.*/ServerName ${MASTER_IP}:80/g' /etc/httpd/conf/httpd.conf
ln -s /usr/share/keystone/wsgi-keystone.conf /etc/httpd/conf.d/wsgi-keystone.conf
systemctl enable httpd.service&& systemctl start httpd.service
export OS_USERNAME=admin
export OS_PASSWORD=admin
export OS_PROJECT_NAME=admin
export OS_USER_DOMAIN_NAME=Default
export OS_PROJECT_DOMAIN_NAME=Default
export OS_AUTH_URL=http://${MASTER_IP}:35357/v3
export OS_IDENTITY_API_VERSION=3
openstack project create --domain default --description "Demo Project" demo
openstack user create --domain default --password $ALL_PASS demo
openstack role create user
openstack role add --project demo --user demo user
openstack project create --domain default --description "Service Project" service
openstack user create --domain default --password $ALL_PASS glance
openstack role add --project service --user glance admin
openstack user create --domain default --password $ALL_PASS nova
openstack role add --project service --user nova admin
创建placement用户
openstack user create --domain default --password $ALL_PASS placement
openstack role add --project service --user placement admin
创建Neutron用户
openstack user create --domain default --password $ALL_PASS neutron
openstack role add --project service --user neutron admin
#创建cinder用户
openstack user create --domain default --password $ALL_PASS cinder
openstack role add --project service --user cinder admin
openstack --os-auth-url http://${MASTER_IP}:35357/v3 --os-project-domain-name default --os-user-domain-name default --os-project-name admin --os-username admin token issue
openstack --os-auth-url http://${MASTER_IP}:5000/v3 --os-project-domain-name default --os-user-domain-name default --os-project-name demo --os-username demo token issue
cat <<EOF > /usr/bin/admin-openstack.sh
export OS_PROJECT_DOMAIN_NAME=Default
export OS_USER_DOMAIN_NAME=Default
export OS_PROJECT_NAME=admin
export OS_USERNAME=admin
export OS_PASSWORD=admin
export OS_AUTH_URL=http://${MASTER_IP}:35357/v3
export OS_IDENTITY_API_VERSION=3
export OS_IMAGE_API_VERSION=2
[root@linux-node1 ~]# vim /root/demo-openstack.sh
export OS_PROJECT_DOMAIN_NAME=Default
export OS_USER_DOMAIN_NAME=Default
export OS_PROJECT_NAME=demo
export OS_USERNAME=demo
export OS_PASSWORD=demo
export OS_AUTH_URL=http://${MASTER_IP}:5000/v3
export OS_IDENTITY_API_VERSION=3
export OS_IMAGE_API_VERSION=2
EOF
source /usr/bin/admin-openstack.sh
yum install -y openstack-glance
